package com.niit.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.niit.bean.Note;
import com.niit.service.NoteService;
import com.niit.service.impl.NoteServiceImpl;
import com.niit.utils.WebKit;

@WebServlet("/AddUpdateNoteController")
public class AddUpdateNoteController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Note note = WebKit.request2Bean(request, Note.class);
		NoteService ns = new NoteServiceImpl();
		String prePath = request.getContextPath();
		if(!"".equals(note.getId())&&note.getId()!=null){
			//如果不为空则表示修改
			if(ns.update(note)>0){
				response.sendRedirect(prePath+"/ShowNoteBookViewController?id="+note.getId());
			}
		}else{
			
			if(ns.add(note)>0){
				//添加成功
				int id = ns.calID4New();
				response.sendRedirect(prePath+"/ShowNoteBookViewController?id="+id);
			}
		}
		
	}

}
